package com.kwan.xframe.util;

import android.Manifest;

import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import timber.log.Timber;


/**
 * 权限管理
 * Created by Mr.Kwan on 2017-8-9.
 * <p>
 * 一、Normal Permissions
 * ACCESS_LOCATION_EXTRA_COMMANDS
 * ACCESS_NETWORK_STATE
 * ACCESS_NOTIFICATION_POLICY
 * ACCESS_WIFI_STATE
 * BLUETOOTH
 * BLUETOOTH_ADMIN
 * BROADCAST_STICKY
 * CHANGE_NETWORK_STATE
 * CHANGE_WIFI_MULTICAST_STATE
 * CHANGE_WIFI_STATE
 * DISABLE_KEYGUARD
 * EXPAND_STATUS_BAR
 * GET_PACKAGE_SIZE
 * INSTALL_SHORTCUT
 * INTERNET
 * KILL_BACKGROUND_PROCESSES
 * MODIFY_AUDIO_SETTINGS
 * NFC
 * READ_SYNC_SETTINGS
 * READ_SYNC_STATS
 * RECEIVE_BOOT_COMPLETED
 * REORDER_TASKS
 * REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
 * REQUEST_INSTALL_PACKAGES
 * SET_ALARM
 * SET_TIME_ZONE
 * SET_WALLPAPER
 * SET_WALLPAPER_HINTS
 * TRANSMIT_IR
 * UNINSTALL_SHORTCUT
 * USE_FINGERPRINT
 * VIBRATE
 * WAKE_LOCK
 * WRITE_SYNC_SETTINGS
 * <p>
 * <p>
 * 二、Dangerous Permissions
 * group:com.google.android.gms.permission.CAR_INFORMATION
 * permission:com.google.android.gms.permission.CAR_VENDOR_EXTENSION
 * permission:com.google.android.gms.permission.CAR_MILEAGE
 * permission:com.google.android.gms.permission.CAR_FUEL
 * <p>
 * group:android.permission-group.CONTACTS
 * permission:android.permission.WRITE_CONTACTS
 * permission:android.permission.GET_ACCOUNTS
 * permission:android.permission.READ_CONTACTS
 * <p>
 * group:android.permission-group.PHONE
 * permission:android.permission.READ_CALL_LOG
 * permission:android.permission.READ_PHONE_STATE
 * permission:android.permission.CALL_PHONE
 * permission:android.permission.WRITE_CALL_LOG
 * permission:android.permission.USE_SIP
 * permission:android.permission.PROCESS_OUTGOING_CALLS
 * permission:com.android.voicemail.permission.ADD_VOICEMAIL
 * <p>
 * group:android.permission-group.CALENDAR
 * permission:android.permission.READ_CALENDAR
 * permission:android.permission.WRITE_CALENDAR
 * <p>
 * group:android.permission-group.CAMERA
 * permission:android.permission.CAMERA
 * <p>
 * group:android.permission-group.SENSORS
 * permission:android.permission.BODY_SENSORS
 * <p>
 * group:android.permission-group.LOCATION
 * permission:android.permission.ACCESS_FINE_LOCATION
 * permission:com.google.android.gms.permission.CAR_SPEED
 * permission:android.permission.ACCESS_COARSE_LOCATION
 * <p>
 * group:android.permission-group.STORAGE
 * permission:android.permission.READ_EXTERNAL_STORAGE
 * permission:android.permission.WRITE_EXTERNAL_STORAGE
 * <p>
 * group:android.permission-group.MICROPHONE
 * permission:android.permission.RECORD_AUDIO
 * <p>
 * group:android.permission-group.SMS
 * permission:android.permission.READ_SMS
 * permission:android.permission.RECEIVE_WAP_PUSH
 * permission:android.permission.RECEIVE_MMS
 * permission:android.permission.RECEIVE_SMS
 * permission:android.permission.SEND_SMS
 * permission:android.permission.READ_CELL_BROADCASTS
 */

public class PermissionUtil {


	public PermissionUtil() {

	}

	public static List<String> mPermissionsNeeded = new ArrayList<>();
	private PermissionListener mPermissionListener;

	public static final String[] VIDEO_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};

	public void requestPermissions(FragmentActivity activity, final String... permissions) {

		RxPermissions rxPermission = new RxPermissions(activity);
		Disposable disposable = rxPermission
				.requestEach(permissions)
				.subscribe(new Consumer<Permission>() {
					@Override
					public void accept(Permission permission) throws Exception {
						if (permission.granted) {
							// 用户已经同意该权限
							Timber.e("%s is granted.", permission.name);
							mPermissionsNeeded.remove(permission.name);
							if (mPermissionListener != null)
								mPermissionListener.onGranted(permission.name);

						} else if (permission.shouldShowRequestPermissionRationale) {
							// 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
							Timber.e("%s is denied. More info should be provided.", permission.name);
							if (mPermissionListener != null)
								mPermissionListener.onShouldShowRequestPermissionRationale(permission.name);
						} else {
							// 用户拒绝了该权限，并且选中『不再询问』
							Timber.e("%s is denied.", permission.name);
							mPermissionsNeeded.add(permission.name);
							if (mPermissionListener != null)
								mPermissionListener.onDenied(permission.name);
						}
					}
				});
	}

	public void requestAllPermission(FragmentActivity activity, final String... permissions) {
		RxPermissions rxPermission = new RxPermissions(activity);
		Disposable disposable = rxPermission
				.request(permissions)
				.subscribe(new Consumer<Boolean>() {

					@Override
					public void accept(@NonNull Boolean granted) throws Exception {
						if (granted) {
							Timber.e("All granted");
						} else {
							Timber.e("At least one permission is denied");
						}
					}
				});
	}

	public boolean isAllPermissionGranted(ArrayList<String> permissions) {
		return !new HashSet<>(mPermissionsNeeded).containsAll(permissions);
	}

	public interface PermissionListener {

		void onGranted(String permission);

		void onShouldShowRequestPermissionRationale(String permission);

		void onDenied(String permission);
	}

	public void setPermissionListener(PermissionListener listener) {
		mPermissionListener = listener;
	}

}
